package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Advertisement;
import com.ruoyi.system.service.AdvertisementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： AdvertisementController
 *  创建时间：2025/2/12
 */
@RestController
@RequestMapping("/Advertisement")
public class AdvertisementController {

    @Resource
    private AdvertisementService advertisementService;

    //查询
    @PostMapping("/get/Advertisement")
    public AjaxResult getAdvertisement(@RequestBody Advertisement advertisement){
        //创建分页对象
        Page<Advertisement> page = new Page<>();
        //设置页码
        page.setCurrent(advertisement.getCurrent());
        //设置行数
        page.setSize(advertisement.getSize());
        //执行查询
        advertisementService.page(page,new LambdaQueryWrapper<Advertisement>()
                //广告文字内容
                .like(StringUtils.isNotBlank(advertisement.getAdvContent()),Advertisement::getAdvContent,advertisement.getAdvContent())
                //审核状态
                .eq(Objects.nonNull(advertisement.getStatus()),Advertisement::getStatus,advertisement.getStatus())
                //逻辑删除
                .eq(Advertisement::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Advertisement::getCreateTime)
                //添加分页语句
                .last(" limit " + (advertisement.getCurrent() - 1) * advertisement.getSize() +"," + advertisement.getSize())
        );
        page.setTotal(advertisementService.count(new LambdaQueryWrapper<Advertisement>()
                //广告文字内容
                .like(StringUtils.isNotBlank(advertisement.getAdvContent()),Advertisement::getAdvContent,advertisement.getAdvContent())
                //审核状态
                .eq(Objects.nonNull(advertisement.getStatus()),Advertisement::getStatus,advertisement.getStatus())
                //逻辑删除
                .eq(Advertisement::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Advertisement::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Advertisement/{id}")
    public AjaxResult deleteAdvertisement(@PathVariable(value = "id")Long id){
        //调用Service层删除
        advertisementService.update(null,new UpdateWrapper<Advertisement>().lambda()
                //主键id
                .eq(Advertisement::getId,id)
                //设置逻辑删除为 真
                .set(Advertisement::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }

    //增加
    @PostMapping("/insert/Advertisement")
    public AjaxResult insertAdvertisement(@RequestBody Advertisement advertisement){
        //设置默认值
        advertisement.setCreateBy(SecurityUtils.getUsername());
        advertisement.setCreateTime(new Date());
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        advertisement.setUpdateTime(new Date());
        advertisement.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        advertisementService.save(advertisement);
        //返回结果
        return AjaxResult.success("新增成功");
    }

    //修改
    @PostMapping("/update/Advertisement")
    public AjaxResult updateAdvertisement(@RequestBody Advertisement advertisement){
        //设置默认值
        advertisement.setUpdateTime(new Date());
        advertisement.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        advertisementService.updateById(advertisement);
        //返回结果
        return AjaxResult.success("修改成功");
    }
}